//var isIE = Object.hasOwnProperty.call(window, "ActiveXObject") && !window.ActiveXObject;

// To identify page tabs. For use mainly with changeActivePageTab()
PageTab = { HOME: 0, PRODUCTS: 1, PROFILE: 2, BILLING: 3, BUY: 4, WORKSPACES: 5, NOTIFICATIONS: 6, DOMAINS: 7, BRANDING: 8, ACCOUNT: 9, SETUP: 10 };

(function() {
    if (!window.$axpage) window.$axpage = {};

    $(document).ready(function() {
        $(window).click(function(event) {
            $('.rowActionMenu').remove();
        });
    });

    // Update speech bubble position to be centered and underneath anchor
    $axpage.updateBubblePosition = function($bubble, $anchor) {
        var position = $anchor.offset();
        var width = parseInt($anchor.css('width').replace('px', ''));
        var height = parseInt($anchor.css('height').replace('px', ''));

        var offset = $bubble.hasClass('axClearMsgBubble') ? 1 : 7;

        $bubble.css('top', (position.top + height + offset) + 'px');
        $bubble.css('left', (position.left + (width / 2) - (parseInt($bubble.css('width').replace('px', '')) / 2)) + 'px');
    }

    // Copy text into clipboard
    $axpage.copyToClipboard = function ($btnElement, $textElement, $anchor, $copySuccess, $copyFailed, event) {
        var $keyText;

        if($textElement.is(':not("input[type="text"]")')) {
            var input = document.createElement("input");
            input.type = "text";
            input.style.cssText = "position: absolute;";
            //input.style = "display: none;";
            $textElement.parent()[0].appendChild(input);
            $textElement.parent().find('input').val($textElement.text());
            $textElement.parent().find('input').text($textElement.text());

            $keyText = $textElement.parent().find('input');
        } else {
            $keyText = $textElement;
        }

        $keyText.select();

        try {
            document.execCommand('copy');

            if (document.selection) {
                document.selection.empty();
            } else if (window.getSelection) {
                window.getSelection().removeAllRanges();
            }

            // Copy confirmation
            if($copySuccess != null) {
                $copySuccess.show();
                $copySuccess.fadeOut(2000, function() {
                    $copySuccess.hide();
                });
            } else {
                var speech = "";
                speech += "<div class='axContextSpeechBubble axSpeechBubble'>";
                speech += "     <span>Copied to clipboard</span>";
                speech += "</div>";

                $btnElement.parent().append(speech);

                $axpage.updateBubblePosition($('.axContextSpeechBubble'), $anchor);

                $('.axContextSpeechBubble').fadeOut(2000, function() {
                    $('.axContextSpeechBubble').hide();
                    $('.axContextSpeechBubble').remove();
                });
            }
        } catch (err) {
            $keyText.select();

            //  Copy error
            if($copyFailed != null) {
                $copyFailed.show();
                $copyFailed.fadeOut(2000, function() {
                    $copyFailed.hide();
                });
            } else {
                var speech = "";
                speech += "<div class='axContextSpeechBubble axSpeechBubble'>";
                speech += "     <span>Could not copy to clipboard</span>";
                speech += "</div>";

                $btnElement.parent().append(speech);

                $axpage.updateBubblePosition($('.axContextSpeechBubble'), $textElement);

                $('.axContextSpeechBubble').fadeOut(2000, function () {
                    $('.axContextSpeechBubble').hide();
                    $('.axContextSpeechBubble').remove();
                });
            }
        }

        $textElement.parent().find('input').remove();

        event.stopPropagation();
    }

    // Append an action menu to a given cell and row
    $axpage.appendActionMenu = function($parent, $cell, contents, event) {
        if($parent.find('.rowActionMenu').length !== 0) {
            event.stopPropagation();
            return;
        }

        // TODO: Clear and hide any other error messages, popups, or function buttons
        $('.rowActionMenu').remove();

        var menu = "";
        menu += "<div class='rowActionMenu disposableBubble'>";
        menu += contents;
        menu += "</div>";

        $cell.append(menu);

        // Update position
        var $menu = $cell.find('.rowActionMenu');
        var position = $cell.offset();
        var width = parseInt($cell.css('width').replace('px', ''));
        var height = parseInt($cell.css('height').replace('px', ''));

        $menu.css('top', (position.top + (3 * (height / 4))) + 'px');
        $menu.css('left', (position.left + (width / 2) - parseInt($menu.css('width').replace('px', ''))) + 'px');

        $menu.click(function(e) { e.stopPropagation(); });
        event.preventDefault();
        event.stopPropagation();
    }

	// Update the active page tab 
	$axpage.changeActivePageTab = function (tab) {
		$('.activeTab').removeClass('activeTab');

    	switch (tab) {
	        case PageTab.HOME:
	            $('#topnav_home').addClass('activeTab');
	            break;
	        case PageTab.PRODUCTS:
	            $('#topnav_products').addClass('activeTab');
	            break;
	        case PageTab.PROFILE:
	            $('#topnav_profile').addClass('activeTab');
	            break;
	        case PageTab.BILLING:
	            $('#topnav_billing').addClass('activeTab');
	            break;
	        case PageTab.BUY:
	            $('#topnav_buy').addClass('activeTab');
	            break;
	        case PageTab.WORKSPACES:
	        	$('#topnav_prototype').addClass('activeTab');
	        	break;
	        case PageTab.NOTIFICATIONS:
	        	$('#topnav_notification').addClass('activeTab');
	        	break;
	        case PageTab.DOMAINS:
	        	$('#topnav_domain').addClass('activeTab');
	        	break;
	        case PageTab.BRANDING:
	        	$('#topnav_branding').addClass('activeTab');
	        	break;
	        case PageTab.ACCOUNT:
	        	$('#topnav_account').addClass('activeTab');
	        	break;
	        case PageTab.SETUP:
	        	$('#topnav_setup').addClass('activeTab');
	        	break;
	    }
	}

	// Show/Hide the PageTab Nav
	$axpage.hidePageTabNav = function(bool) {
		if (bool) {
            $('#topNav').css('visibility', 'hidden');
        } else {
            $('#topNav').css('visibility', 'visible');
        }
	}

	// Show/Hide the Profile Header (Thumbnail, username, help/logout links)
    $axpage.hideProfileHeader = function(bool) {
        if (bool) {
            $('#profileHeader').css('visibility', 'hidden');
        } else {
            $('#profileHeader').css('visibility', 'visible');
        }
    }
})();